package com.itheima.service.impl;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.Code;
import com.itheima.common.MessageConstant;
import com.itheima.common.SetmealContext;
import com.itheima.dto.SetmealDTO;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.exception.HeimaExceptionThrow;
import com.itheima.mapper.SetmealDishMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: fan
 * @Desc: 套餐模块
 * @Date: 2022/6/5 11:45
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 功能描述: 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return : com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.itheima.domain.Setmeal>
     */
    @Override
    public Page<Setmeal> page(Integer page, Integer pageSize, String name) {
        if (page < 1) {
            page = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like(Setmeal::getName, name.trim());
        }
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<Setmeal> selectPage = setmealMapper.selectPage(setmealPage, queryWrapper);
        return selectPage;
    }

    /**
     * 功能描述: 修改套餐时数据回显
     *
     * @param id
     * @return : com.itheima.domain.SetmealDTO
     */
    @Override
    public SetmealDTO findById(Long id) {
        SetmealDTO setmealDTO = new SetmealDTO();
        if (id == null) {
            return setmealDTO;
        }

        Setmeal setmeal = setmealMapper.selectById(id);
        if (setmeal != null) {
            BeanUtils.copyProperties(setmeal, setmealDTO);
        }
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(queryWrapper);
        if (setmealDishes != null) {
            setmealDTO.setSetmealDishes(setmealDishes);
        }
        return setmealDTO;


    }

    /**
     * 功能描述: 修改套餐
     *
     * @param setmealDTO
     * @return : int
     */
    @Transactional
    @Override
    public int updateSetmeal(SetmealDTO setmealDTO) {
        int row = 0;
        if (setmealDTO == null) {
            return row;
        }
        //套餐直接修改
        Setmeal setmeal = new Setmeal();
        //验证是否为中文字、英文字母、数字和下划线
        String name = setmealDTO.getName();
        if (StringUtils.isBlank(name) || !Validator.isGeneralWithChinese(name)) {
            // return row;
            HeimaExceptionThrow.bussinessException(Code.ERROR, MessageConstant.SETMEAL_VERIFY_NAME);
        }
        //验证是否为数字
        BigDecimal price = setmealDTO.getPrice();
        int scale = price.scale();
        if (price == null || scale != 0) {
            // return row;
            HeimaExceptionThrow.bussinessException(Code.ERROR, MessageConstant.SETMEAL_VERIFY_PRICE);

        }
        BeanUtils.copyProperties(setmealDTO, setmeal);
        row = setmealMapper.updateById(setmeal);
        if (row == 0) {
            return row;
        }
        //菜品先删除再新增
        Long setmealId = setmeal.getId();
        if (setmealId != null) {
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId, setmealId);
            setmealDishMapper.delete(wrapper);
        }
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null) {
            for (SetmealDish setmealDish : setmealDishes) {
                //防止出现主键冲突问题
                setmealDish.setId(null);
                //前端参数中没有套餐ID，手动设置一下
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }

        return row;
    }

    /**
     * 功能描述: 新增套餐
     *
     * @param setmealDTO
     * @return : int
     */
    @Transactional
    @Override
    public int addSetmeal(SetmealDTO setmealDTO) {
        int row = 0;
        if (setmealDTO == null) {
            return row;
        }
        Setmeal setmeal = new Setmeal();
        //验证是否为中文字、英文字母、数字和下划线
        String name = setmealDTO.getName();
        if (StringUtils.isBlank(name) || !Validator.isGeneralWithChinese(name)) {
            // return row;
            HeimaExceptionThrow.bussinessException(Code.ERROR, MessageConstant.SETMEAL_VERIFY_NAME);
        }
        //验证是否为数字
        BigDecimal price = setmealDTO.getPrice();
        int scale = price.scale();
        if (price == null || scale != 0) {
            // return row;
            HeimaExceptionThrow.bussinessException(Code.ERROR, MessageConstant.SETMEAL_VERIFY_PRICE);

        }

        BeanUtils.copyProperties(setmealDTO, setmeal);
        if (setmeal != null) {
            row = setmealMapper.insert(setmeal);
        }
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Long setmealId = setmeal.getId();
        if (setmealDishes != null) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
                setmealDishMapper.insert(setmealDish);
            }
        }
        return row;
    }

    /**
     * 功能描述: 套餐停售,起售功能
     *
     * @param status
     * @param ids
     * @return : int
     */
    @Override
    public int updeupdateSetmealStatus(Integer status, Long[] ids) {
        int row = 0;
        if (status == null || ids == null) {
            return row;
            //  HeimaExceptionThrow.bussinessException(Code.ERROR, "批量操作，请先勾选操作套餐！");
        }
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Setmeal::getStatus, status);
        updateWrapper.in(Setmeal::getId, ids);
        Setmeal setmeal = new Setmeal();
        row = setmealMapper.update(setmeal, updateWrapper);
        return row;
    }

    /**
     * 功能描述: 逻辑删除,批量删除
     *
     * @param ids
     * @return : int
     */
    @Override
    public int deleteSetmeal(Long[] ids) {
        int row = 0;
        if (ids == null) {
            return row;
            //    HeimaExceptionThrow.bussinessException(Code.ERROR, "您没有勾选菜品,么得办法删!");
        }
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Setmeal::getIsDeleted, SetmealContext.IS_DELETED);
        updateWrapper.in(Setmeal::getId, ids);
        Setmeal setmeal = new Setmeal();
        row = setmealMapper.update(setmeal, updateWrapper);
        return row;
    }

}
